CM3D2 Converter.tex_import
1import os 2import bpy 3from . import common 4from . import compat 5from .translations.pgettext_functions import * 6 7 8@compat.BlRegister() 9class CNV_OT_import_cm3d2_tex(bpy.types.Operator): 10 bl_idname = 'image.import_cm3d2_tex' 11 bl_label = "texファイルを開く" 12 bl_description = "CM3D2で使用されるテクスチャファイル(.tex)を読み込みます" 13 bl_options = {'REGISTER'} 14 15 filepath = bpy.props.StringProperty(subtype='FILE_PATH') 16 filename_ext = ".tex;.png" 17 filter_glob = bpy.props.StringProperty(default="*.tex;*.png", options={'HIDDEN'}) 18 19 items = [ 20 ('PACK', "内部にパックする", "", 'PACKAGE', 1), 21 ('PNG', "PNGに変換してPNGを開く", "", 'IMAGE_DATA', 2), 22 ] 23 mode = bpy.props.EnumProperty(items=items, name="展開方法", default='PNG') 24 25 def invoke(self, context, event): 26 prefs = common.preferences() 27 if prefs.tex_default_path: 28 self.filepath = common.default_cm3d2_dir(prefs.tex_default_path, None, "tex") 29 else: 30 self.filepath = common.default_cm3d2_dir(prefs.tex_import_path, None, "tex") 31 context.window_manager.fileselect_add(self) 32 return {'RUNNING_MODAL'} 33 34 def draw(self, context): 35 box = self.layout.box() 36 col = box.column(align=True) 37 col.label(text="展開方法", icon='FILESEL') 38 col.prop(self, 'mode', icon='FILESEL', expand=True) 39 40 def execute(self, context): 41 common.preferences().tex_import_path = self.filepath 42 try: 43 tex_data = common.load_cm3d2tex(self.filepath) 44 if tex_data is None: 45 # bpy.ops.image.open(filepath=self.filepath) 46 # img = context.edit_image 47 self.report(type={'ERROR'}, message="texファイルのヘッダが正しくありません。" + self.fielpath) 48 return {'CANCELLED'} 49 50 tex_format = tex_data[1] 51 if not (tex_format == 3 or tex_format == 5): 52 self.report(type={'ERROR'}, message="未対応フォーマットのtexです。format=" + str(tex_format)) 53 return {'CANCELLED'} 54 55 root, ext = os.path.splitext(self.filepath) 56 png_path = root + ".png" 57 is_png_overwrite = os.path.exists(png_path) 58 if self.mode == 'PACK' and is_png_overwrite: 59 png_path += ".temp.png" 60 with open(png_path, 'wb') as png_file: 61 png_file.write(tex_data[-1]) 62 bpy.ops.image.open(filepath=png_path) 63 img = context.edit_image 64 img.name = os.path.basename(self.filepath) 65 img['cm3d2_path'] = common.get_tex_cm3d2path(root + ".png") 66 67 if self.mode == 'PACK': 68 img.pack(as_png=True) 69 os.remove(png_path) 70 return {'FINISHED'} 71 72 except: 73 self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath)) 74 return {'CANCELLED'} 75 76 77# メニューを登録する関数 78def menu_func(self, context): 79 self.layout.separator() 80 self.layout.operator(CNV_OT_import_cm3d2_tex.bl_idname, icon_value=common.kiss_icon())
@compat.BlRegister()
class
CNV_OT_import_cm3d2_tex9@compat.BlRegister() 10class CNV_OT_import_cm3d2_tex(bpy.types.Operator): 11 bl_idname = 'image.import_cm3d2_tex' 12 bl_label = "texファイルを開く" 13 bl_description = "CM3D2で使用されるテクスチャファイル(.tex)を読み込みます" 14 bl_options = {'REGISTER'} 15 16 filepath = bpy.props.StringProperty(subtype='FILE_PATH') 17 filename_ext = ".tex;.png" 18 filter_glob = bpy.props.StringProperty(default="*.tex;*.png", options={'HIDDEN'}) 19 20 items = [ 21 ('PACK', "内部にパックする", "", 'PACKAGE', 1), 22 ('PNG', "PNGに変換してPNGを開く", "", 'IMAGE_DATA', 2), 23 ] 24 mode = bpy.props.EnumProperty(items=items, name="展開方法", default='PNG') 25 26 def invoke(self, context, event): 27 prefs = common.preferences() 28 if prefs.tex_default_path: 29 self.filepath = common.default_cm3d2_dir(prefs.tex_default_path, None, "tex") 30 else: 31 self.filepath = common.default_cm3d2_dir(prefs.tex_import_path, None, "tex") 32 context.window_manager.fileselect_add(self) 33 return {'RUNNING_MODAL'} 34 35 def draw(self, context): 36 box = self.layout.box() 37 col = box.column(align=True) 38 col.label(text="展開方法", icon='FILESEL') 39 col.prop(self, 'mode', icon='FILESEL', expand=True) 40 41 def execute(self, context): 42 common.preferences().tex_import_path = self.filepath 43 try: 44 tex_data = common.load_cm3d2tex(self.filepath) 45 if tex_data is None: 46 # bpy.ops.image.open(filepath=self.filepath) 47 # img = context.edit_image 48 self.report(type={'ERROR'}, message="texファイルのヘッダが正しくありません。" + self.fielpath) 49 return {'CANCELLED'} 50 51 tex_format = tex_data[1] 52 if not (tex_format == 3 or tex_format == 5): 53 self.report(type={'ERROR'}, message="未対応フォーマットのtexです。format=" + str(tex_format)) 54 return {'CANCELLED'} 55 56 root, ext = os.path.splitext(self.filepath) 57 png_path = root + ".png" 58 is_png_overwrite = os.path.exists(png_path) 59 if self.mode == 'PACK' and is_png_overwrite: 60 png_path += ".temp.png" 61 with open(png_path, 'wb') as png_file: 62 png_file.write(tex_data[-1]) 63 bpy.ops.image.open(filepath=png_path) 64 img = context.edit_image 65 img.name = os.path.basename(self.filepath) 66 img['cm3d2_path'] = common.get_tex_cm3d2path(root + ".png") 67 68 if self.mode == 'PACK': 69 img.pack(as_png=True) 70 os.remove(png_path) 71 return {'FINISHED'} 72 73 except: 74 self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath)) 75 return {'CANCELLED'}
filepath: <_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}> =
<_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}>
filter_glob: <_PropertyDeferred, <built-in function StringProperty>, {'default': '*.tex;*.png', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}> =
<_PropertyDeferred, <built-in function StringProperty>, {'default': '*.tex;*.png', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}>
mode: <_PropertyDeferred, <built-in function EnumProperty>, {'items': [('PACK', '内部にパックする', '', 'PACKAGE', 1), ('PNG', 'PNGに変換してPNGを開く', '', 'IMAGE_DATA', 2)], 'name': '展開方法', 'default': 'PNG', 'attr': 'mode'}> =
<_PropertyDeferred, <built-in function EnumProperty>, {'items': [('PACK', '内部にパックする', '', 'PACKAGE', 1), ('PNG', 'PNGに変換してPNGを開く', '', 'IMAGE_DATA', 2)], 'name': '展開方法', 'default': 'PNG', 'attr': 'mode'}>
def
invoke(self, context, event):
26 def invoke(self, context, event): 27 prefs = common.preferences() 28 if prefs.tex_default_path: 29 self.filepath = common.default_cm3d2_dir(prefs.tex_default_path, None, "tex") 30 else: 31 self.filepath = common.default_cm3d2_dir(prefs.tex_import_path, None, "tex") 32 context.window_manager.fileselect_add(self) 33 return {'RUNNING_MODAL'}
def
execute(self, context):
41 def execute(self, context): 42 common.preferences().tex_import_path = self.filepath 43 try: 44 tex_data = common.load_cm3d2tex(self.filepath) 45 if tex_data is None: 46 # bpy.ops.image.open(filepath=self.filepath) 47 # img = context.edit_image 48 self.report(type={'ERROR'}, message="texファイルのヘッダが正しくありません。" + self.fielpath) 49 return {'CANCELLED'} 50 51 tex_format = tex_data[1] 52 if not (tex_format == 3 or tex_format == 5): 53 self.report(type={'ERROR'}, message="未対応フォーマットのtexです。format=" + str(tex_format)) 54 return {'CANCELLED'} 55 56 root, ext = os.path.splitext(self.filepath) 57 png_path = root + ".png" 58 is_png_overwrite = os.path.exists(png_path) 59 if self.mode == 'PACK' and is_png_overwrite: 60 png_path += ".temp.png" 61 with open(png_path, 'wb') as png_file: 62 png_file.write(tex_data[-1]) 63 bpy.ops.image.open(filepath=png_path) 64 img = context.edit_image 65 img.name = os.path.basename(self.filepath) 66 img['cm3d2_path'] = common.get_tex_cm3d2path(root + ".png") 67 68 if self.mode == 'PACK': 69 img.pack(as_png=True) 70 os.remove(png_path) 71 return {'FINISHED'} 72 73 except: 74 self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath)) 75 return {'CANCELLED'}
Inherited Members
- bpy_types.Operator
- as_keywords
- poll_message_set
- builtins.bpy_struct
- keys
- values
- get
- pop
- as_pointer
- keyframe_insert
- keyframe_delete
- driver_add
- driver_remove
- is_property_set
- property_unset
- is_property_readonly
- is_property_overridable_library
- property_overridable_library_set
- path_resolve
- path_from_id
- type_recast
- bl_rna_get_subclass_py
- bl_rna_get_subclass
- id_properties_ensure
- id_properties_clear
- id_properties_ui
- id_data